package com.gqj.bas.controller;

import com.gqj.bas.domain.BasCabinet;
import com.gqj.bas.service.IBasCabinetService;
import com.gqj.common.annotation.Log;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.core.page.TableDataInfo;
import com.gqj.common.enums.BusinessType;
import com.gqj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 密品柜子Controller
 *
 * @author lxq
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/bas/basCabinet")
public class BasCabinetController extends BaseController
{
    @Autowired
    private IBasCabinetService basCabinetService;

    /**
     * 查询密品柜子列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basCabinet:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasCabinet basCabinet)
    {
        startPage();
        basCabinet.setDelFlag("0");
        List<BasCabinet> list = basCabinetService.selectBasCabinetList(basCabinet);
        return getDataTable(list);
    }

    /**
     * 查询柜架列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basCabinet:cabinetOptions')")
    @GetMapping("/cabinetOptions")
    public AjaxResult cabinetOptions(BasCabinet basCabinet)
    {
        List<Map<String, Object>> list = basCabinetService.cabinetOptions(basCabinet);
        return AjaxResult.success(list);
    }

    /**
     * 导出密品柜子列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basCabinet:export')")
    @Log(title = "密品柜子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasCabinet basCabinet)
    {
        List<BasCabinet> list = basCabinetService.selectBasCabinetList(basCabinet);
        ExcelUtil<BasCabinet> util = new ExcelUtil<BasCabinet>(BasCabinet.class);
        util.exportExcel(response, list, "密品柜子数据");
    }

    /**
     * 获取密品柜子详细信息
     */
    @PreAuthorize("@ss.hasPermi('bas:basCabinet:query')")
    @GetMapping(value = "/{cabinetId}")
    public AjaxResult getInfo(@PathVariable("cabinetId") Long cabinetId)
    {
        return AjaxResult.success(basCabinetService.selectBasCabinetByCabinetId(cabinetId));
    }

    /**
     * 新增密品柜子
     */
    @PreAuthorize("@ss.hasPermi('bas:basCabinet:add')")
    @Log(title = "密品柜子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasCabinet basCabinet)
    {
        if (UserConstants.NOT_UNIQUE.equals(basCabinetService.checkCabinetCodeUnique(basCabinet)))
        {
            return AjaxResult.error("新增柜子'" + basCabinet.getCabinetName() + "'失败，设备编码已存在");
        }
        if (UserConstants.NOT_UNIQUE.equals(basCabinetService.checkCabinetIpUnique(basCabinet)))
        {
            return AjaxResult.error("新增柜子'" + basCabinet.getCabinetName() + "'失败，ip已存在");
        }
        basCabinet.setCreateBy(getUsername());
        return toAjax(basCabinetService.insertBasCabinet(basCabinet));
    }

    /**
     * 修改密品柜子
     */
    @PreAuthorize("@ss.hasPermi('bas:basCabinet:edit')")
    @Log(title = "密品柜子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasCabinet basCabinet)
    {
        if (UserConstants.NOT_UNIQUE.equals(basCabinetService.checkCabinetCodeUnique(basCabinet)))
        {
            return AjaxResult.error("修改柜子'" + basCabinet.getCabinetName() + "'失败，设备编码已存在");
        }
        if (UserConstants.NOT_UNIQUE.equals(basCabinetService.checkCabinetIpUnique(basCabinet)))
        {
            return AjaxResult.error("修改柜子'" + basCabinet.getCabinetName() + "'失败，ip已存在");
        }
        basCabinet.setUpdateBy(getUsername());
        return toAjax(basCabinetService.updateBasCabinet(basCabinet));
    }

    /**
     * 删除密品柜子
     */
    @PreAuthorize("@ss.hasPermi('bas:basCabinet:remove')")
    @Log(title = "密品柜子", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cabinetIds}")
    public AjaxResult remove(@PathVariable Long[] cabinetIds)
    {
        for(Long cabinetId: cabinetIds){
            if (basCabinetService.checkGridExistCabinet(cabinetId))
            {
                return AjaxResult.error("柜子已被引用,不允许删除");
            }
        }
        return toAjax(basCabinetService.deleteBasCabinetByCabinetIds(cabinetIds));
    }
}
